<template>
    <div style="">
        <div class="container" v-show="!showEdit">
            <div class="handle-box">
                <!-- <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button> -->
                <el-input v-model="title" placeholder="标题" class="handle-input mr10"></el-input>
                <el-date-picker
                    v-model="timeRange"
                    type="daterange"
                    range-separator="-"
                    start-placeholder="创建时间"
                    end-placeholder="创建时间"
                    value-format="yyyy-MM-dd"
                    style="width: 230px; margin-right: 10px"
                >
                </el-date-picker>
                <el-select v-model="status" placeholder="状态" class="handle-select mr10">
                    <el-option key="1" label="启用" value="1"></el-option>
                    <el-option key="0" label="禁用" value="0"></el-option>
                </el-select>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
            </div>
            <div style=" display: flex;justify-content: flex-end;margin-bottom: 20px;">
                <el-button style="width:80px" @click="order">排序</el-button>
                <el-button type="primary" style="width:80px" @click="addAccount">新建</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <el-table-column type="index" :index="methodIndex" label="序号" width="55" align="center"></el-table-column>
                <el-table-column prop="title" label="标题"></el-table-column>
                <el-table-column label="创建时间" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.createTime | dateFilter }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="状态" width="80" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" :class="{ red: scope.row.status == 0 }" @click="handleStatus(scope.$index, scope.row)">{{
                            scope.row.status == 0 ? '禁用' : '启用'
                        }}</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" v-if="scope.row.status == 0" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" @click="handlePassword(scope.$index, scope.row)">预览</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next,jumper"
                    :current-page="pageNo"
                    :page-size="pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <edit-banner v-if="showEdit" :isAdd="isAdd" :bannerId="bannerId" @back="back" @save="save"></edit-banner>

        <!-- 状态弹出框 -->
        <el-dialog class="popView" :visible.sync="editStatus" v-if="editStatus" :show-close="false" width="200px">
            <div class="main">
                <span class="title">请确认是否{{ selItem.status == 0 ? '启用' : '禁用' }}</span>
                <div class="btns">
                    <el-button @click="editStatus = false">取 消</el-button>
                    <el-button type="primary" @click="saveStatus">确认</el-button>
                </div>
            </div>
        </el-dialog>
        <!-- 排序 -->
        <el-dialog class="popView popView1" :visible.sync="isOrder" v-if="isOrder" width="400px" title="排序">
            <div class="main">
                <div style=" display: flex;justify-content: flex-end;margin-bottom: 20px;width:100%">
                    <el-button type="primary" style="width:80px" @click="saveOrder">保存</el-button>
                </div>
                <el-table :data="orderTable" border class="table">
                    <el-table-column type="index" :index="methodIndex" label="序号" width="55" align="center"></el-table-column>
                    <el-table-column prop="title" label="标题"></el-table-column>
                    <el-table-column label="操作" width="180" align="center">
                        <template slot-scope="scope">
                            <el-button type="text" v-if="scope.$index != 0" @click="clickOrder(true, scope.$index, scope.row)"
                                >向上</el-button
                            >
                            <el-button
                                type="text"
                                v-if="scope.$index != orderTable.length - 1"
                                @click="clickOrder(false, scope.$index, scope.row)"
                                >向下</el-button
                            >
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </el-dialog>
        <!-- 查看弹出框 -->
        <el-dialog class="popView" :visible.sync="isShow" v-if="isShow" :show-close="false" width="375px">
            <div class="main">
                <span class="title">{{ selItem.title }}</span>
                <iframe class="web" :srcdoc="selItem.content" frameborder="0"></iframe>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import editBanner from '../common/editBanner';
import EditBanner from '../common/editBanner.vue';
export default {
    data() {
        return {
            tableData: [],
            multipleSelection: [],
            delList: [],

            title: null,
            createTime: null,
            status: null,
            pageNo: 1,
            pageSize: 10,
            editStatus: false,
            selItem: null,
            pageTotal: 0,
            resetPassword: false,
            showEdit: false,
            isAdd: false,
            bannerId: 0,
            orderTable: [],
            isOrder: false,
            isShow: false,
            timeRange:null
        };
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            this.$api
                .post({
                    url: '/article/pageQuery',
                    data: {
                        pageNo: this.pageNo,
                        pageSize: this.pageSize,
                        param: {
                            title: this.title,
                            status: this.status,
                            createTimeStart: this.timeRange ? this.timeRange[0] + ' 00:00:00' : null,
                            createTimeEnd: this.timeRange ? this.timeRange[1] + ' 00:00:00' : null
                        }
                    }
                })
                .then(res => {
                    if (res.code && res.code == '1001') {
                    } else {
                        this.tableData = res.data.result || [];
                        this.pageTotal = res.data.total;
                    }
                });
        },
        order() {
            this.$api
                .post({
                    url: '/article/pageQuery',
                    data: {
                        pageNo: 1,
                        pageSize: 10,
                        param: {
                            status: 1
                        }
                    }
                })
                .then(res => {
                    if (res.code && res.code == '1001') {
                    } else {
                        this.orderTable = res.data.result || [];
                        this.isOrder = true;
                    }
                });
        },
        clickOrder(isUp, index, row) {
            if (isUp) {
                let a = this.orderTable[index];
                let b = this.orderTable[index - 1];
                this.$set(this.orderTable, index, b);
                this.$set(this.orderTable, index - 1, a);
            } else {
                let a = this.orderTable[index];
                let b = this.orderTable[index + 1];
                this.$set(this.orderTable, index, b);
                this.$set(this.orderTable, index + 1, a);
            }
        },
        saveOrder() {
            let list = this.orderTable.map((item, index) => {
                return { id: item.id, orderNum: index + 1 };
            });
            console.log('🚀 ~ file: PortalReportBanner.vue ~ line 207 ~ saveOeder ~ list', list);

            // return;
            this.$api
                .post({
                    url: '/article/changeOrderNum',
                    data: {
                        list: list
                    }
                })
                .then(res => {
                    if (res.code && res.code == '200') {
                        this.$message({
                            message: '保存完成',
                            type: 'success'
                        });
                        this.isOrder = false;
                        this.pageNo = 1;
                        this.pageSize = 10;
                        this.getData();
                    }
                });
        },
        // 触发搜索按钮
        handleSearch() {
            this.pageNo = 1;
            this.getData();
        },
        // 重置密码
        handlePassword(index, item) {
            this.selItem = item;
            this.isShow = true;
        },
        reSet() {
            this.$api
                .adminUserResetPw({
                    loginNo: this.selItem.loginNo
                })
                .then(res => {
                    this.resetPassword = false;
                });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 设置状态
        handleStatus(index, item) {
            this.selItem = item;
            this.editStatus = true;
        },
        saveStatus() {
            this.$api
                .get({
                    url: '/article/changeStatus',
                    data: {
                        id: this.selItem.id,
                        status: this.selItem.status == 1 ? 0 : 1
                    }
                })
                .then(res => {
                    if (res.code == 200) {
                        this.getData();
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                    this.editStatus = false;
                });
        },
        // 编辑操作
        handleEdit(index, row) {
            this.showEdit = true;
            this.bannerId = row.id;
            this.isAdd = false;
        },
        // 新增
        addAccount() {
            this.showEdit = true;
            this.isAdd = true;
            this.bannerId = 0;
        },
        back() {
            this.selItem = null;
            this.showEdit = false;
        },
        // 保存
        save() {
            this.selItem = null;
            this.showEdit = false;
            this.getData();
        },
        // 分页导航
        handlePageChange(val) {
            this.pageNo = val;
            this.getData();
        },
        methodIndex(index) {
            return index + 1;
        }
    },
    components: {
        editBanner
    }
};
</script>

<style lang="scss" scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 100px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
/deep/.popView {
    display: flex;
    align-items: center;
    justify-content: center;
    .el-dialog__header {
        padding: 0;
    }
    .el-dialog__body {
        padding: 0;
    }
    .main {
        display: flex;
        flex-direction: column;
        align-items: center;
        background-color: #fff;
        border-radius: 5px;
        padding: 20px 0;
        .title {
            font-size: 17px;
            font-weight: bold;
            margin-bottom: 20px;
        }
        .btns {
            width: 100%;
            display: flex;
            justify-content: space-around;
        }
    }
}
/deep/.popView1 {
    .el-dialog__header {
        padding-top: 10px;
        text-align: center;
    }
    .el-dialog__body {
        padding: 0 20px 20px 20px;
    }
}
</style>
